﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;

namespace CalemShop
{
    public class BackupUtil
    {
        public static RegistryKey calemRegistry = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default);
        public static RegistryKey backupDataRegistry = null;
        public static string doCheckFolderBackup()
        {
            string bkPath = Application.ExecutablePath;
            bkPath = Path.GetDirectoryName(bkPath);
            DateTime d = DateTime.Now;
            bkPath = Path.Combine(bkPath, "data", "SkyDrive", RuntimeData.GetBranchId(), "backup", d.Year.ToString(), d.Month.ToString());
            DirectoryInfo backupDir = new DirectoryInfo(bkPath);
            if (!Directory.Exists(bkPath))
            {
                backupDir = Directory.CreateDirectory(bkPath);
            }

            return bkPath;

        }

        public static string doCheckFolderInvoice()
        {
            string invoicePath = Application.ExecutablePath;
            invoicePath = Path.GetDirectoryName(invoicePath);
            DateTime d = DateTime.Now;
            invoicePath = Path.Combine(invoicePath, "data", "SkyDrive", RuntimeData.GetBranchId(), "invoices", d.Year.ToString(), d.Month.ToString(), d.Day.ToString());
            DirectoryInfo invoiceDir = new DirectoryInfo(invoicePath);
            if (!Directory.Exists(invoicePath))
            {
                invoiceDir = Directory.CreateDirectory(invoicePath);
            }

            return invoicePath;

        }


        public static string doCheckWorkspace()
        {
            string stockPath = Application.ExecutablePath;
            stockPath = Path.GetDirectoryName(stockPath);
            DateTime d = DateTime.Now;
            stockPath = Path.Combine(stockPath, "data", "workspace");
            DirectoryInfo invoiceDir = new DirectoryInfo(stockPath);
            if (!Directory.Exists(stockPath))
            {
                invoiceDir = Directory.CreateDirectory(stockPath);
            }

            return stockPath;

        }

        public static bool doBackupData()
        {
            if (backupDataRegistry == null)
            {
                backupDataRegistry = calemRegistry.CreateSubKey(@"software\Calem\backup\daily");
            }

            string bkKey = DateTime.Now.ToString("dd-MM-yyyy hh");
            if (backupDataRegistry != null)
            {
                string value = (string)backupDataRegistry.GetValue("backup", string.Empty);

                if (string.IsNullOrEmpty(value) == false && string.Compare(value, bkKey, true) == 0)
                {
                    return true;
                }
            }


            string bkPath = doCheckFolderBackup();
            string fileName = string.Format("calemshop_{0}.sql", DateTime.Now.ToString("MM-dd-HH-ss"));
            string bkFile = Path.Combine(bkPath, fileName);
            try
            {

                ProcessStartInfo proc = new ProcessStartInfo();
                string cmd = string.Format(@"-r {4} -u{0} -p{1} -h{2} {3}", "root", "rroott", "localhost", "calemshop", bkFile);
                proc.FileName = RuntimeData.GetMysqlDump();
                proc.RedirectStandardInput = false;
                proc.RedirectStandardOutput = true;
                proc.CreateNoWindow = true;
                proc.Arguments = cmd;
                proc.UseShellExecute = false;
                Process p = Process.Start(proc);
                p.WaitForExit();
                if (backupDataRegistry != null)
                {
                    backupDataRegistry.SetValue("backup", bkKey);
                }
                return true;
            }

            catch (IOException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            return false;
        }

        public static bool doSynData(string dataPath)
        {
            try
            {
                string scriptPath = Application.ExecutablePath;
                scriptPath = Path.GetDirectoryName(scriptPath);
                DateTime d = DateTime.Now;
                scriptPath = Path.Combine(scriptPath, "data", "script", "syn.bat");
                ProcessStartInfo proc = new ProcessStartInfo();
                proc.FileName = scriptPath;
                proc.RedirectStandardInput = false;
                proc.RedirectStandardOutput = false;
                proc.CreateNoWindow = true;
                proc.Arguments = dataPath;
                proc.UseShellExecute = false;
                Process p = Process.Start(proc);
                p.WaitForExit();
                return true;
            }

            catch (IOException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

            return false;
        }
    }
}
